import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
import _objectWithoutProperties from '@babel/runtime/helpers/esm/objectWithoutProperties';
import _toConsumableArray from '@babel/runtime/helpers/esm/toConsumableArray';
import { Transfer as AntdTransfer, Tree } from 'antd';
import React, { useMemo } from 'react';
import createField from '../../../createField';
var _excluded = ['children'],
  _excluded2 = ['children', 'dataSource', 'targetKeys', 'treeProps'];
var generateTree = function generateTree() {
  var treeNodes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var checkedKeys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  return treeNodes.map(function (_ref) {
    var children = _ref.children,
      props = _objectWithoutProperties(_ref, _excluded);
    return _objectSpread(
      _objectSpread({}, props),
      {},
      {
        disabled: checkedKeys.includes(props.key),
        children: generateTree(children, checkedKeys),
      },
    );
  });
};
var TableTransfer = function TableTransfer(_ref2) {
  var children = _ref2.children,
    dataSource = _ref2.dataSource,
    _ref2$targetKeys = _ref2.targetKeys,
    targetKeys = _ref2$targetKeys === void 0 ? [] : _ref2$targetKeys,
    _ref2$treeProps = _ref2.treeProps,
    treeProps = _ref2$treeProps === void 0 ? {} : _ref2$treeProps,
    rest = _objectWithoutProperties(_ref2, _excluded2);
  var transferDataSource = useMemo(
    function () {
      var result = [];
      function flatten() {
        var list = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
        list.forEach(function (item) {
          result.push(item);
          if (item.children) flatten(item.children);
        });
      }
      flatten(dataSource);
      return result;
    },
    [dataSource],
  );
  return /*#__PURE__*/ React.createElement(
    AntdTransfer,
    _objectSpread(
      _objectSpread({}, rest),
      {},
      {
        targetKeys: targetKeys,
        dataSource: transferDataSource,
      },
    ),
    function (_ref3) {
      var direction = _ref3.direction,
        onItemSelect = _ref3.onItemSelect,
        selectedKeys = _ref3.selectedKeys;
      if (direction === 'left') {
        var checkedKeys = [].concat(
          _toConsumableArray(selectedKeys),
          _toConsumableArray(targetKeys),
        );
        return /*#__PURE__*/ React.createElement(
          Tree,
          _objectSpread(
            _objectSpread(
              {
                blockNode: true,
                checkable: true,
                checkStrictly: true,
                defaultExpandAll: true,
              },
              treeProps,
            ),
            {},
            {
              checkedKeys: checkedKeys,
              treeData: generateTree(dataSource, targetKeys),
              onCheck: function onCheck(_, _ref4) {
                var key = _ref4.node.key;
                onItemSelect(key, !checkedKeys.includes(key));
              },
              onSelect: function onSelect(_, _ref5) {
                var key = _ref5.node.key;
                onItemSelect(key, !checkedKeys.includes(key));
              },
            },
          ),
        );
      }
    },
  );
};
export default createField(TableTransfer, {
  defaultFormItemProps: {
    valuePropName: 'targetKeys',
  },
});
